import { useAxiosWithAuth } from "@/utils/useAxiosWidthAuth";
import { useEffect, useState } from "react";

/**
 * 获取商品详情
 * @param {String} id - 商品ID
 */

export const useFindGoods = function (params: any) {
  const axios = useAxiosWithAuth();
  const [result, setResult] = useState<any>();
  useEffect(() => {
    async function findGoods() {
      const res: any = await axios({
        method: "get",
        url: "/goods",
        params,
      });
      setResult(res.result);
    }
    findGoods();
  }, []);
  return { result };
};

// 获取同类商品推荐
/**
 * 获取商品同类推荐-未传入ID为猜喜欢
 * @param {String} id - 商品ID
 * @param {Number} limit - 获取条数
 */
export const useFindRelGoods = function (id: string, limit: number) {
  const axios = useAxiosWithAuth();
  const [result, setResult] = useState<any>();
  useEffect(() => {
    async function findRelGoods() {
      const res: any = await axios({
        method: "get",
        url: "/goods/relevant",
        params: { id, limit },
      });
      setResult(res.result);
    }
    findRelGoods();
  }, [id, limit]);
  return { result };
};

/**
 * 获取热榜商品
 * @param {Number} type - 1代表24小时热销榜 2代表周热销榜 3代表总热销榜
 * @param {Number} limit - 获取个数
 */
export const useFindHotGoods = function (id: string, type: number, limit = 3) {
  const axios = useAxiosWithAuth();
  const [result, setResult] = useState<any>();
  useEffect(() => {
    async function findHotGoods() {
      const res: any = await axios({
        method: "get",
        url: "/goods/hot",
        params: { id, limit, type },
      });
      setResult(res.result);
    }
    findHotGoods();
  }, [id, limit, type]);
  return { result };
};
